
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#include "miniutil/ref_ptr.h"
#include "miniutil/thread.h"
#include "miniutil/threadpool.h"
#include "miniutil/random.h"
#include "minitest/testcase.h"

#include <iostream>

#include <vector>
#include <map>

using namespace miniutil;
using namespace miniutil::data;

#define TEST_SIZE 200000

class refshareclass:public miniutil::runnable
{
public:
    refshareclass(std::string name, int looptime)
    {
        mname = name;
        mloop = looptime;
    }

    void SetShareValue(miniutil::data::ref_ptr<int> intvalue)
    {
        mivalue = intvalue;
    }

    void run()
    {
        int i=0; 
        while(true)
        {
            (*(mivalue)) ++;
            std::cout<<mname<<":"<<*(mivalue)<<"["<<mivalue.count()<<"] ";
            miniutil::thread::sleep(miniutil::Random::rand_value(1000));
            if(i >= mloop)
                break;
            i++;
        }
        mivalue = miniutil::data::ref_ptr<int>::safenew(10);
    };

    miniutil::data::ref_ptr<int> mivalue;
    std::string mname;
    int mloop;
};

class refptrtest1 : public testcase
{

public: 
	refptrtest1()
	{
		themain.tests.push_back( this );
	}

    virtual void test()
    {
        test2();
    }

    virtual void test2()
    {
        refshareclass a("A", 10);
        refshareclass b("B", 5);

        miniutil::data::ref_ptr<int> pdata = miniutil::data::ref_ptr<int>::safenew(0);

        a.SetShareValue(pdata);
        b.SetShareValue(pdata);

        miniutil::threadpool::RunTask(&a);
        miniutil::threadpool::RunTask(&b);

        while(true)
        {
            miniutil::thread::sleep(100);
        }
    }

	virtual void test1()
	{
		cout << "test refptrtest1 ... "<< endl;

		//cout << "\ntest invalid constructor...\t\t\t";
		//try {
		//	miniutil::data::ref_ptr<int> a( );
		//	cout << "\n\t test Failed";
		//}catch( miniutil::data::ref_ptr_error & e)
		//{
		//	cout << "\n\t test OK\t msg is:" << e.what() << std::endl;
		//}


		cout << "test refptr as vector/map ";
		std::vector< miniutil::data::ref_ptr<int> > aaa;
		std::map< miniutil::data::ref_ptr<int>, miniutil::data::ref_ptr<std::string> > tmap;

		cout << "\t\tOK\n";

		miniutil::data::ref_ptr<int> k ( DO_NEW,  0 );
		miniutil::data::ref_ptr<std::string>  v ( DO_NEW, "haha");

		miniutil::data::ref_ptr<int> k2(k);
		
		cout << "test map \t\t";
		tmap[k] = v;
		miniutil::data::ref_ptr<int> nullvalue;

		if ( tmap[k2]->compare("haha") == 0 )
			cout << "OK\n";
		else
			cout << "Fail\n";


//		miniutil::data::ref_ptr<int>  *p = new miniutil::data::ref_ptr<int> ;
//		miniutil::data::ref_ptr<int>  *p = new miniutil::data::ref_ptr<int>[] ;
		miniutil::data::ref_ptr<int>  *p = &k ;


	}
};

testcase * trefptr = new refptrtest1();


